﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using EpiNEXT.Services.Fields;

namespace EpiNEXT.Services
{
    /// <summary>
    /// A class representing a page on a data entry form
    /// </summary>
    //[Serializable()]
    public class Page
    {
        #region Members
        private string _title = String.Empty;
        private Form _form;
        #endregion // Members

        #region Properties
        [XmlIgnore]
        public bool IsInDesignMode { get; set; }

        /// <summary>
        /// The ID that represents this page
        /// </summary>
        [XmlElement("Id")]
        public Guid PageId { get; set; }

        /// <summary>
        /// The position of this page
        /// </summary>
        [XmlElement("Position")]
        public int Position { get; set; }

        /// <summary>
        /// The title for the page
        /// </summary>
        [XmlElement("Title")]
        public string Title
        {
            get
            {
                return this._title;
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    throw new InvalidOperationException("Page title cannot be blank.");
                }
                else if (value.Length > 64)
                {
                    throw new InvalidOperationException("Page title cannot exceed 64 characters.");
                }
                else if (!System.Text.RegularExpressions.Regex.IsMatch(value, @"\w"))
                {
                    throw new InvalidOperationException("Page title must include only letters, numbers, and underscores.");
                }
                else
                {
                    double firstChar = -1;
                    bool success = double.TryParse(value[0].ToString(), out firstChar);
                    if (success)
                    {
                        throw new InvalidOperationException("Page title may not start with a digit.");
                    }

                    this._title = value;
                }
            }
        }

        [XmlIgnore]
        public ObservableCollection<IField> Fields { get; private set; }

        [XmlIgnore]
        public Form Owner
        {
            get
            {
                return this._form;
            }
            set
            {
                this._form = value;
                UpdateFields();
            }
        }
        #endregion // Properties

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        [Obsolete("Use of default constructor not allowed", true)]
        private Page() { }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="owner">The form that owns this page</param>
        public Page(Form owner)
        {
            this.Owner = owner;
            this.Title = "New Page";
            this.PageId = Guid.NewGuid();
            this.Position = Owner.GetNextPagePosition();
        }
        #endregion // Constructors

        #region Methods
        public void UpdateFields()
        {
            Fields = new ObservableCollection<IField>();
            foreach (IField field in Owner.Fields)
            {
                if (field.PageId.Equals(this.PageId))
                {
                    Fields.Add(field);
                }
            }
        }
        #endregion // Methods
    }
}
